package com.jh3502.vivo.function.reimbursepages;

import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jh3502.vivo.base.BaseCallback;
import com.jh3502.vivo.base.BaseJson;
import com.jh3502.vivo.base.BasePresenter;
import com.jh3502.vivo.base.BaseView;
import com.jh3502.vivo.function.todo.TodoListBean;
import com.jh3502.vivo.utils.Utils;

import java.util.List;
import java.util.Map;

/**
 * @author harisucici
 * @date 2017/6/19
 */

public class ReimListPresenter extends BasePresenter<ReimListPresenter.ReimListView> {
    ReimListModel mReimListModel;

    ReimListView mReimListView;


    public void setReimListView(ReimListView mReimListView) {
        this.mReimListView = mReimListView;

    }

    public void toGetMyTask(Map<String, Object> body) {
        mReimListView.showLoadingDialog();
        if (mReimListModel == null) {
            mReimListModel = new ReimListModel(mReimListView.getActContext());
        }

        mReimListModel.getMyTask(body, new BaseCallback(mReimListView) {
            @Override
            public void responseSuccess(@NonNull BaseJson baseJson) {
                String rows = (String) Utils.getInstance().jso2map(baseJson.getObj().toString()).get("rows");
                mReimListView.getMyTasklSuccess(jsa2List(rows));
            }
        });
    }

    public void toFindAuditTask(Map<String, Object> body) {
        mReimListView.showLoadingDialog();
        if (mReimListModel == null) {
            mReimListModel = new ReimListModel(mReimListView.getActContext());
        }

        mReimListModel.findAuditTask(body, new BaseCallback(mReimListView) {
            @Override
            public void responseSuccess(@NonNull BaseJson baseJson) {
                String s = (String) (Utils.getInstance().jso2map(baseJson.getObj().toString())).get("rows");
                List<TodoListBean> dataList = new Gson().fromJson(s,
                        new TypeToken<List<TodoListBean>>() {
                        }.getType());
                mReimListView.findAuditTaskSuccess(dataList);
            }
        });
    }

    public void toAgreeForAll(List<Map<String, Object>> body) {
        mReimListView.showLoadingDialog();
        if (mReimListModel == null) {
            mReimListModel = new ReimListModel(mReimListView.getActContext());
        }

        mReimListModel.agreeForAll(body, new BaseCallback(mReimListView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {

                mReimListView.agreeForAllSuccess();
            }
        });
    }

    public void toDeleteTask(Map<String, Object> body) {
        mReimListView.showLoadingDialog();
        if (mReimListModel == null) {
            mReimListModel = new ReimListModel(mReimListView.getActContext());
        }

        mReimListModel.deleteTask(body, new BaseCallback(mReimListView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {

                mReimListView.deleteTaskSuccess();
            }
        });
    }


    public void toDeleteAudit(Map<String, Object> body) {
        mReimListView.showLoadingDialog();
        if (mReimListModel == null) {
            mReimListModel = new ReimListModel(mReimListView.getActContext());
        }

        mReimListModel.deleteAudit(body, new BaseCallback(mReimListView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {

                mReimListView.deleteAuditSuccess();
            }
        });
    }

    public List<TodoListBean> jsa2List(String s) {
        return new Gson().fromJson(s,
                new TypeToken<List<TodoListBean>>() {
                }.getType());
    }


    @Override
    public void cancelCall() {

    }

    public interface ReimListView extends BaseView {

        void getMyTasklSuccess(List<TodoListBean> TodoListBeans);

        void findAuditTaskSuccess(List<TodoListBean> dataList);

        void agreeForAllSuccess();

        void deleteAuditSuccess();

        void deleteTaskSuccess();
    }
}
